package com.dek.ssm.ch3.utils;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionFactoryUtils {
	
	private final static Class<SqlSessionFactoryUtils> LOCK = SqlSessionFactoryUtils.class;
	
	private static SqlSessionFactory sqlSessionFactory = null;
	
	private SqlSessionFactoryUtils(){}
	
	public static SqlSessionFactory getSqlSessionFactory() {
		synchronized (LOCK) {
			if (sqlSessionFactory == null) {
				String resource = "mybatis-config.xml";
				InputStream inputStream;
				try {
					inputStream = Resources.getResourceAsStream(resource);
					sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
			return sqlSessionFactory;
		}
	}
		
	public static SqlSession openSqlSession() {
		if (sqlSessionFactory == null) {
			getSqlSessionFactory();
		}
		return sqlSessionFactory.openSession();
	}
}
